package com.zy_demo;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.zygame.sdk.CmCode;
import com.zygame.sdk.ConstantValue;
import com.zygame.sdk.ZyGameSDK;
import com.zygame.sdk.bean.GameServerParams;
import com.zygame.sdk.bean.PayResult;
import com.zygame.sdk.bean.UserExtraData;
import com.zygame.sdk.bean.ZY_PayParams;
import com.zygame.sdk.interfaces.InitResult;
import com.zygame.sdk.interfaces.ZySDKListener;
import com.zygame.sdk.model.UserModel;
import com.zygame.sdk.utils.SDKTools;
import com.zygame.sdk.utils.log.LogHelper;
import com.zygame.sdk.view.ZY_BaseActivity;
import com.zygame.sdk.view.dialog.Zy_Dialog_main;

import org.json.JSONObject;

public class MainActivity extends ZY_BaseActivity implements OnClickListener, GoogleApiClient.OnConnectionFailedListener {

    private IWXAPI api;
    private JSONObject jsonObject;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //
        /*
         * 设置调试debug
         *
         * true : 有日志调试 flase : 无日志
         */
        // CmGameSDK.getInstance().setDebug(true);
        // CmGameSDK.getInstance().setUrlDebug(true);
        setListener(new int[]{R.id.btn_login, R.id.btn_float_window, R.id.btn_submit_data, R.id.btn_create_role, R.id.btn_channel, R.id.btn_out, R.id.game_role, R.id.btn_register, R.id.btn_chage, R.id.btn_pay, R.id.btn_g});
        findViewById(R.id.btn_wechat).setOnClickListener(this);


        //微信
        //api = WXAPIFactory.createWXAPI(this,ConstantValue.APP_ID,false);
        //api.registerApp(ConstantValue.APP_ID);
//        CmGameSDK.getInstance().setSDKListener(cmsdkListener); // 必须 设置监听

        ZyGameSDK.getInstance().init(this, zySDKListener); // 必须， 初始化接口
        ZyGameSDK.getInstance().setDebug(true);

    }




    ZySDKListener zySDKListener = new ZySDKListener() {

        @Override
        public void onInitResult(InitResult result) { // 初始化完成回调
            LogHelper.i("mainA", "MAINiNIT");
            ZyGameSDK.getInstance().login();//登录
            Toast.makeText(MainActivity.this, "初始化成功", Toast.LENGTH_SHORT).show();
        }


        @Override
        public void onPayResult(PayResult result) { // 支付回调
            int code = result.getResultCode();
            if (code == CmCode.CODE_PAY_SUCCESS) { // 支付成功
                Toast.makeText(MainActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
            } else if (code == CmCode.CODE_PAY_CANCEL) { // 支付取消
                Toast.makeText(MainActivity.this, "取消支付", Toast.LENGTH_SHORT).show();
            } else if (code == CmCode.CODE_PAY_FAIL) { // 支付失败
                Toast.makeText(MainActivity.this, "支付失败: " + result.getExtension(), Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onLogout() { //账号注销
            ZyGameSDK.getInstance().hideFloatButton();
        }

        @Override
        public void onLoginResult(UserModel userModel) { // 登录回调
            LogHelper.i("回调", userModel.toString());


            if (userModel.isLogin()) {// 是否登录
                Toast.makeText(MainActivity.this, "欢迎 " + userModel.getLogin_account() + "回来！", Toast.LENGTH_SHORT).show();
                String uid = userModel.getUid();//Uid
                String name = userModel.getLogin_account();//用户名
                String logintoken = userModel.getLogin_Token();//登录token
                ZyGameSDK.getInstance().showFloatButton(MainActivity.this, true, 0, 0.1f);//开悬浮窗
            }
        }

        @Override
        public void onUpData(InitResult result) {
            if (result.isUpData()) {//有新的jar包
                String url = result.getUplaodUrl();//
            }
        }
    };


    public void setListener(int ids[]) {
        int length = ids.length;
        for (int i = 0; i < length; i++) {
            findViewById(ids[i]).setOnClickListener(this);
        }
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.btn_login:
                //CmGameSDK.getInstance().login();
                ZyGameSDK.getInstance().login();//登录
                //HttpUtil.login("", "", "3", "", this, onLoginCallback);
                break;

            case R.id.btn_float_window:
                // CmGameSDK.getInstance().showFloatButton(MainActivity.this, true, 0, 0.1f);//开悬浮窗
                // CmGameSDK.getInstance().hideFloatButton();
                Toast.makeText(this, "cehsi ", Toast.LENGTH_SHORT).show();
                break;

            case R.id.btn_submit_data:
                //Intent serviceIntent = new Intent(this, HeartbeatService.class);
                //stopService(serviceIntent);
                //submitData();
                break;
            case R.id.btn_create_role:
                //createRole();
                ZyGameSDK.getInstance().game_online_time();//角色在线
                break;
            case R.id.btn_channel:
                String str = SDKTools.getLogicChannel(this, ConstantValue.CHANNEL_NAME);
                Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_out:
                ZyGameSDK.getInstance().logout();//注销
                break;
            case R.id.game_role:
                GameServerParams gameServerParams = new GameServerParams();
                gameServerParams.setRoleName("测试");
                gameServerParams.setServerName("测试");
                gameServerParams.setRoleLevel("88");
                gameServerParams.setServerId("125");
                gameServerParams.setRoleId("88");
                ZyGameSDK.getInstance().gameRole(gameServerParams);//选角色
                break;
            case R.id.btn_pay:
                mHelper.enableDebugLogging(true);

                long currentTimeMillis = System.currentTimeMillis();
                ZY_PayParams params = new ZY_PayParams();
                params.setGame_order_id(currentTimeMillis + "");//唯一值
                params.setGame_other("测试");//
                params.setSandbox("1");//商品沙盒
                params.setGoods_id("p3");//商品id
                params.setGoods_type("inapp");//商品类型  "inapp" for one-time purchases and "subs" for subscriptions
                params.setGoods_name("6元套餐");//商品名字
                params.setGoods_price(0.1);//商品价格
                params.setPay_num(1);//商品数量

                ZyGameSDK.getInstance().pay(this, params, mHelper);


                break;
            case R.id.btn_chage:
                ZyGameSDK.getInstance().changeUser();//切换账号
                break;
            case R.id.btn_register:
                ZyGameSDK.getInstance().register();//注册
                break;
            case R.id.btn_g:
//                ZyGameSDK.getInstance().googleSignIn();
                break;
            case R.id.btn_wechat:
                //Toast.makeText(this,"sss",Toast.LENGTH_SHORT).show();
                //final SendAuth.Req req = new SendAuth.Req();
                //req.scope = "snsapi_userinfo";
                //req.state = "wechat_sdk_demo_test";
                //api.sendReq(req);
                //new TestDialog(this).show();
                break;
        }
    }




    // 创角
    private void createRole() {
        UserExtraData userExtra = new UserExtraData();
        userExtra.setRoleName("法师");

        // CmGameSDK.getInstance().createRole(userExtra);
    }

    String orderId = "1445421620100020001";
    double price = 0.1;

    private void pay() {
//        PayParams payParmas = new PayParams();
//        payParmas.setPrice(0.1); // 必须填 价钱
//        payParmas.setServerName("这是demo测试哈哈哈"); // 必须填 服务器名称
//        payParmas.setServerId("1212121");
//        payParmas.setProductName("6元套餐"); // 必须填 商品名称
//        payParmas.setExtension(UUID.randomUUID().toString().replaceAll("-", "")); // 扩展信息
//        // 订单参数之类的
//        payParmas.setOrderID(UUID.randomUUID().toString().replaceAll("-", ""));// 设置订单号


//        ZY_PayParams zy_payParams = new ZY_PayParams();
//        zy_payParams.setGame_order_id("1421");
//        zy_payParams.setGame_other("1421");
//        zy_payParams.setSandbox("1421");
//        zy_payParams.setGoods_id("1421");
//        zy_payParams.setGoods_name("1421");
//        zy_payParams.setGoods_price("1421");
//        zy_payParams.setPay_num("1421");
//        CmGameSDK.getInstance().pay(this, zy_payParams);
    }

    private void submitData() { // 必须
        GameServerParams params = new GameServerParams();
        params.setServerName("这是demo测试哈哈哈"); // 必须填 服名称
        params.setServerId("1002"); // 必须填 服id
        params.setRoleLevel("21"); // 必须填 角色等级
        params.setRoleName("小炮"); // 必须填 角色名称
        ZyGameSDK.getInstance().submitServerData(params);
        Toast.makeText(this, "选服成功 !!!", Toast.LENGTH_SHORT).show();
    }


    @Override
    public void onBackPressed() {
        ZyGameSDK.getInstance().stopSdk();
        ZyGameSDK.getInstance().restart();
        finish();
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        Zy_Dialog_main.getInstance(this).dissDialog();
    }
}
